#!/bin/bash

# Slackware build script for skulltag

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20230111 bkw: BUILD=4, strip libsnes_spc.so
# 20211024 bkw: BUILD=3, binaries in /usr/games, new-style icons

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=skulltag
VERSION=${VERSION:-098d}
BUILD=${BUILD:-4}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  LIBDIRSUFFIX=""
  TARARCH=x86
elif [ "$ARCH" = "i686" ]; then
  LIBDIRSUFFIX=""
  TARARCH=x86
elif [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
  TARARCH=x86_64
else
  echo "Unsupported ARCH '$ARCH'. Supported ARCH values are 'i586' 'i686' 'x86_64'"
  exit 1
fi

set -e

# This is closed-source software. 3 tarballs here:
# BASE: platform-independent data, mostly goes in usr/share/skulltag
# GAME: game binary + a couple of shared libs.
# FMOD: old version of shared lib required for the game bin. The fmodapi
#       from SBo can't be used because it's too new. Note that our default
#       skulltag.ini has fmodapi *disabled* (in favor of emulated OPL), and
#       enabling fmodapi from the in-game menus doesn't work (no music heard).
#       This fmodapi shared lib exists *just* to make the damn game runnable.

BASE=$CWD/st-v${VERSION}_linux-base.tar.bz2
GAME=$CWD/st-v${VERSION}_linux-$TARARCH.tar.bz2
FMOD=$CWD/fmodapi42416linux$LIBDIRSUFFIX.tar.gz

# The shared libs will all be installed in usr/lib$LIBDIRSUFFIX/skulltag/
# so as not to interfere with the system lib dir. The game binary will also go
# there, with a wrapper script to set LD_LIBRARY_PATH.

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
tar xvf $BASE
tar xvf $GAME
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

mkdir -p \
  $PKG/usr/lib$LIBDIRSUFFIX/$PRGNAM \
  $PKG/usr/games \
  $PKG/usr/share/$PRGNAM \
  $PKG/usr/doc/$PRGNAM-$VERSION

# 20230111 bkw: this *one* library isn't already stripped:
strip libsnes_spc.so

mv $PRGNAM $PRGNAM-server *.so $PKG/usr/lib$LIBDIRSUFFIX/$PRGNAM
install -m0644 *.pk3 $PKG/usr/share/$PRGNAM
install -m0644 *.txt $PKG/usr/doc/$PRGNAM-$VERSION

# The announcer and skins stuff seems like it ought to go in
# /usr/share/$PRGNAM (same as the .pk3 files), but strace shows
# that it looks in /usr/lib$LIBDIRSUFFIX/$PRGNAM, and I can't find
# a way to change it in the config file.
chmod 0644 announcer/* skins/*
cp -a announcer skins $PKG/usr/lib$LIBDIRSUFFIX/$PRGNAM

mkdir fmod
cd fmod
tar xvf $FMOD
install -m0755 \
  fmodapi*/api/lib*/libfmodex$LIBDIRSUFFIX-*.so \
  $PKG/usr/lib$LIBDIRSUFFIX/$PRGNAM

sed "s,@LIB@,lib$LIBDIRSUFFIX,g" < $CWD/$PRGNAM.sh > $PKG/usr/games/$PRGNAM
chmod 755 $PKG/usr/games/$PRGNAM
ln -s $PRGNAM $PKG/usr/games/$PRGNAM-server

# The included ini file changes the following program defaults:

# - Sets the skulltag master server to master.skulltag.net (this is the
#   correct skullag_masterip since October 2011).

# - Sets the savegame path to ~/.skulltag. The default savegame path in
#   the skulltag binary is ~.skulltag (missing the slash), which creates
#   savegames in a subdirectory of the current dir (confusing for us
#   CLI users). This issue has been fixed upstream in the current
#   dev snapshot.

# - Searches for wads and other files in /usr/share/skulltag (since
#   that's where this package installs skulltag's .pk3 data) and
#   /usr/share/games/doom (for compatibility with existing SBo zdoom
#   package)

# - Disables FMOD audio (use emulated OPL instead, since FMOD doesn't seem
#   to actually work)

# - Uses ALSA instead of OSS as the default audio output device. OSS has been
#   deprecated for maybe 10 years now, it's beyond me why app developers
#   continue to make it the default.

# The wrapper script will copy the default .ini to ~/.skulltag/skulltag.ini
# if it doesn't already exist.

cat $CWD/$PRGNAM.ini > $PKG/usr/share/$PRGNAM/$PRGNAM.ini.default

# The program ships with no useful documentation. Users are expected to use
# the wiki, so let's give the user the URL.
cat $CWD/README_docs.txt > $PKG/usr/doc/$PRGNAM-$VERSION/README_docs.txt

# 20211024 bkw: icons extracted from the OSX installer:
# http://www.skulltag.com/download/files/release/st-v098d_macosx.dmg
# and a couple were made by resizing 128.png.
for i in $CWD/icons/*.png; do
  px=$( basename $i | cut -d. -f1 )
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  cat $i > $dir/$PRGNAM.png
done

mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# .desktop is a modified copy of zdoom.desktop
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
